package com.googlecode.cswish.util;


import java.util.Locale;

import com.googlecode.cswish.model.PageElements;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.LocalizedTextUtil;

/**
 * Helper class - get the map information intuitively 
 * 
 * @author Jerry.Feng
 */
public class ActionContextHelper {
	
	public static void setPageStructure(PageElements ps) {
		ActionContext context = ActionContext.getContext();
		context.put(FrameConstant.STACK_PAGEELEMENTS, ps);
	}

	public static PageElements getPageStructure() {
		ActionContext context = ActionContext.getContext();
		PageElements ps = (PageElements) context.get(FrameConstant.STACK_PAGEELEMENTS);
		return ps;
	}
	
	public static Functions getFunctions() {
		ActionContext context = ActionContext.getContext();
		Functions fn = (Functions) context.get(FrameConstant.STACK_FUNCTION);
		return fn;
	}
	
	public static String getText(String str) {
		Locale locale = ActionContext.getContext().getLocale();
		String value = LocalizedTextUtil.findDefaultText(str, locale);
		if (value == null) {
			int index = str.lastIndexOf('.');
			if (index > 0) {
				value = LocalizedTextUtil.findDefaultText(str.substring(index + 1), locale);
			}
		}
		return value == null ? str : value;
	}
	
	public static String getText(Class clazz, String key, Locale locale, Object... args) {
		if (locale == null) {
			locale = ActionContext.getContext().getLocale();
		}
		String value = LocalizedTextUtil.findText(clazz, key, locale, key, args);
		return value;
	}
}
